/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AltaLote.java
 *
 * Created on 05-may-2011, 12:02:57
 */

package interfaces.farmacia;

import Controlador.Controlador;
import GestorFarmacia.Medicamento;
import clinica.ClinicaView;
import clinica.Interfaces;
import clinica.Interfaz;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jdesktop.application.Action;

/**
 *
 * @author leynar
 */
public class AltaLote extends javax.swing.JPanel {
    private ClinicaView app;
    private Interfaces _anterior;
    private Medicamento _medicamento;

    /** Creates new form altaModLote */
    public AltaLote(ClinicaView a) {
        app = a;
        initComponents();
    }

    @Action
    public void cancelar() {
        app.volver(Interfaces.BUSCAR_MEDICAMENTO_2); // linkar a la interfaz anterior (ANA)
    }
    
    @Action
    public void aceptar() throws IOException, ClassNotFoundException {
        String exito_lote = null;
        Pattern p = Pattern.compile("[^0-9]+"); // Solo numeros!
        Matcher m = p.matcher(unidad_ini.getText().toString());
        boolean error = false;
        boolean errorFechas = false;
        app.limpiaErrores();


        if (m.find() || unidad_ini.getText().isEmpty()) {
            error = true;
            p = Pattern.compile("^-.*"); // Solo numeros!
            m = p.matcher(unidad_ini.getText().toString());
            if(unidad_ini.getText().isEmpty())
                app.addError("Debe introducir las unidades iniciales");
            else if(m.find())
                app.addError("Las unidades iniciales no pueden ser negativas");
            else
                app.addError("Las unidades iniciales deben de ser cifras");
        }
        m = p.matcher(unidad_res.getText().toString());
        if(m.find() || unidad_res.getText().isEmpty()) {
            error = true;
            p = Pattern.compile("^-.*"); // Solo numeros!
            m = p.matcher(unidad_res.getText().toString());
            if(unidad_res.getText().isEmpty())
                app.addError("Debe introducir las unidades restantes");
            else if(m.find())
                app.addError("Las unidades restantes no pueden ser negativas");
            else
                app.addError("Las unidades restantes deben de ser cifras");
        }
        if(fecha_entrada.getDate() == null) {
            error = true;
            errorFechas = true;
            app.addError("Debe introducir una fecha de entrada del lote");
        }
        if(fecha_cad.getDate() == null) {
            error = true;
            errorFechas = true;
            app.addError("Debe introducir una fecha de caducidad del lote");
        }

        if(!errorFechas) {
            if(fecha_cad.getDate().before(fecha_entrada.getDate())) {
                error = true;
                app.addError("La fecha de salida no puede ser anterior a la de entrada");
            }
        }

        if(error){
            app.muestraListaErrores();
            app.limpiaErrores();}
        else {
            exito_lote = Controlador.altaLote(fecha_cad.getDate(), fecha_entrada.getDate(),
                                                Integer.parseInt(unidad_ini.getText()), Integer.parseInt(unidad_res.getText()),
                                                this._medicamento.getIdMedicamento());
            if(exito_lote == null) {
                app.abrirMensajeExito("El lote ha sido dado de alta correctamente", Interfaces.BUSCAR_MEDICAMENTO_2);//Falta conectarla con la de ANA
            }
            else {
                app.abrirMensajeError(exito_lote, Interfaces.BUSCAR_MEDICAMENTO_2);//Falta conectarla con la de ANA
            }
        }
    }

    public void rellenaDatos(Medicamento m, Interfaces anterior) {
        this._medicamento = m;
        this._anterior = anterior;
        medicamento.setText(m.getNombreMedicamento());
    }

    public void limpiaDatos() {
        medicamento.setText("");
        unidad_ini.setText("");
        unidad_res.setText("");
        fecha_entrada.setDate(null);
        fecha_cad.setDate(null);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel21 = new javax.swing.JPanel();
        unidad_ini = new javax.swing.JTextField();
        unidad_res = new javax.swing.JTextField();
        medicamento = new javax.swing.JTextField();
        jLabel50 = new javax.swing.JLabel();
        jLabel51 = new javax.swing.JLabel();
        jLabel52 = new javax.swing.JLabel();
        jLabel53 = new javax.swing.JLabel();
        jLabel54 = new javax.swing.JLabel();
        jPanel20 = new javax.swing.JPanel();
        fecha_entrada = new com.toedter.calendar.JDateChooser();
        fecha_cad = new com.toedter.calendar.JDateChooser();
        aceptar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();

        setFont(Interfaz.setFontSize());

        jPanel21.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize()));
        jPanel21.setName("jPanel21"); // NOI18N

        unidad_ini.setFont(Interfaz.setFontSize());
        unidad_ini.setToolTipText("");
        unidad_ini.setName("unidad_ini"); // NOI18N

        unidad_res.setFont(Interfaz.setFontSize());
        unidad_res.setToolTipText("");
        unidad_res.setName("unidad_res"); // NOI18N

        medicamento.setBackground(new java.awt.Color(204, 255, 255));
        medicamento.setEditable(false);
        medicamento.setFont(Interfaz.setFontSize());
        medicamento.setToolTipText("");
        medicamento.setName("medicamento"); // NOI18N

        jLabel50.setFont(Interfaz.setFontSize());
        jLabel50.setText("Medicamento");
        jLabel50.setName("jLabel50"); // NOI18N

        jLabel51.setFont(Interfaz.setFontSize());
        jLabel51.setText("Fecha de salida");
        jLabel51.setName("jLabel51"); // NOI18N

        jLabel52.setFont(Interfaz.setFontSize());
        jLabel52.setText("Fecha de entrada");
        jLabel52.setName("jLabel52"); // NOI18N

        jLabel53.setFont(Interfaz.setFontSize());
        jLabel53.setText("Unidades restantes");
        jLabel53.setName("jLabel53"); // NOI18N

        jLabel54.setFont(Interfaz.setFontSize());
        jLabel54.setText("Unidades iniciales");
        jLabel54.setName("jLabel54"); // NOI18N

        jPanel20.setName("jPanel20"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel20Layout = new org.jdesktop.layout.GroupLayout(jPanel20);
        jPanel20.setLayout(jPanel20Layout);
        jPanel20Layout.setHorizontalGroup(
            jPanel20Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 163, Short.MAX_VALUE)
        );
        jPanel20Layout.setVerticalGroup(
            jPanel20Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 6, Short.MAX_VALUE)
        );

        fecha_entrada.setName("fecha_entrada"); // NOI18N

        fecha_cad.setName("fecha_cad"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel21Layout = new org.jdesktop.layout.GroupLayout(jPanel21);
        jPanel21.setLayout(jPanel21Layout);
        jPanel21Layout.setHorizontalGroup(
            jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel21Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel21Layout.createSequentialGroup()
                        .add(jPanel20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(236, 236, 236))
                    .add(jPanel21Layout.createSequentialGroup()
                        .add(jLabel50)
                        .add(54, 54, 54)
                        .add(medicamento, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(jPanel21Layout.createSequentialGroup()
                        .add(jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel54)
                            .add(jLabel53)
                            .add(jLabel51))
                        .add(18, 18, 18)
                        .add(jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, fecha_cad, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                            .add(unidad_res, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                            .add(unidad_ini, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE))
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel21Layout.createSequentialGroup()
                        .add(jLabel52)
                        .add(32, 32, 32)
                        .add(fecha_entrada, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanel21Layout.setVerticalGroup(
            jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel21Layout.createSequentialGroup()
                .add(jPanel20, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(14, 14, 14)
                .add(jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(medicamento, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel50))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(fecha_entrada, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel52))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(fecha_cad, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel51))
                .add(18, 18, 18)
                .add(jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel54)
                    .add(unidad_ini, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel21Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel53)
                    .add(unidad_res, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(26, 26, 26))
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(AltaLote.class, this);
        aceptar.setAction(actionMap.get("aceptar")); // NOI18N
        aceptar.setFont(new java.awt.Font("Tahoma", 0, 18));
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(AltaLote.class);
        aceptar.setIcon(resourceMap.getIcon("aceptar.icon")); // NOI18N
        aceptar.setText(resourceMap.getString("jButton3.text")); // NOI18N
        aceptar.setToolTipText(resourceMap.getString("jButton3.toolTipText")); // NOI18N
        aceptar.setLabel(resourceMap.getString("jButton3.label")); // NOI18N
        aceptar.setName("aceptar"); // NOI18N

        jButton3.setAction(actionMap.get("cancelar")); // NOI18N
        jButton3.setFont(new java.awt.Font("Tahoma", 0, 18));
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setToolTipText(resourceMap.getString("jButton3.toolTipText")); // NOI18N
        jButton3.setLabel(resourceMap.getString("jButton3.label")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 296, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 8, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(684, Short.MAX_VALUE)
                .add(jButton3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(aceptar))
            .add(layout.createSequentialGroup()
                .add(70, 70, 70)
                .add(jPanel21, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(38, 38, 38))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(306, Short.MAX_VALUE)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(248, 248, 248))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(59, 59, 59)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 80, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton3)
                    .add(aceptar)))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private com.toedter.calendar.JDateChooser fecha_cad;
    private com.toedter.calendar.JDateChooser fecha_entrada;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel50;
    private javax.swing.JLabel jLabel51;
    private javax.swing.JLabel jLabel52;
    private javax.swing.JLabel jLabel53;
    private javax.swing.JLabel jLabel54;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel20;
    private javax.swing.JPanel jPanel21;
    private javax.swing.JTextField medicamento;
    private javax.swing.JTextField unidad_ini;
    private javax.swing.JTextField unidad_res;
    // End of variables declaration//GEN-END:variables

}
